@page "/billing/manage"

@using Passwordless.AdminConsole.Billing.Configuration
@using Microsoft.Extensions.Options

@inject ISharedBillingService BillingService;
@inject IDataService DataService;
@inject IOptions<BillingOptions> BillingOptions;
@inject NavigationManager NavigationManager;
@inject ICurrentContext CurrentContext;

<Page Title="Manage Billing">
    @if (_isInitialized)
    {
        @if (!CurrentContext.Organization!.HasSubscription)
        {
            <Panel Header="Upgrade to add multiple applications">
                <p>Your Free Organization includes 1 Trial app (up to 10k users) and 1 console administrator.</p>
                <EditForm FormName="@UpgradeProFormName" Model="UpgradeProForm" OnSubmit="@OnUpgradeProFormSubmittedAsync">
                    <button name="UpgradeProForm.Action" class="btn-primary" type="submit" value="Upgrade">Upgrade to a Paid Organization</button>
                </EditForm>
                <p class="text-xs">Your Trial app will be upgraded to a Pro app. Estimated variable cost $@(Math.Round(Applications!.Select(a => a.Users).Sum() * 0.05, 2))/month. See <a class="link-blue" href="https://bitwarden.com/products/passwordless/#pricing" target="_blank">Pricing page</a> for details.</p>
            </Panel>
        }

        <Panel Header="Applications">
            <EditForm FormName="@ApplicationsFormName" Model="ApplicationsForm" OnSubmit="@OnApplicationsFormSubmitted">
                <Table ColumnHeaders="@ApplicationsHeaders" EmptyMessage="@ApplicationsEmptyMessage">
                    @foreach (var app in Applications!)
                    {
                        <tr>
                            <td>@app.Id</td>
                            <td>@app.Description</td>
                            <td>@app.Users</td>
                            <td>@app.Plan</td>
                            <td>
                                @if (app.CanChangePlan)
                                {
                                    <input type="hidden" name="ApplicationsForm.Action" value="ChangePlan" />
                                    <button name="ApplicationsForm.ApplicationId" value="@app.Id" type="submit" class="btn-primary">
                                        <PencilSquareIcon class="h-6 w-6" />
                                    </button>
                                }
                            </td>
                        </tr>
                    }
                </Table>
            </EditForm>
        </Panel>

        @if (CurrentContext.Organization.HasSubscription)
        {
            <Panel Header="Payment Methods">
                <div class="space-y-4 sm:space-y-0 sm:flex sm:flex-wrap sm:gap-4 sm:grid sm:grid-cols-1">
                    <p>Manage &amp; update your payment information and billing details.</p>
                    <EditForm FormName="@ChangePaymentMethodFormName" Model="ChangePaymentMethodForm" OnSubmit="@OnChangePaymentMethodFormSubmittedAsync">
                        <button name="ChangePaymentMethodForm.Action" type="submit" class="btn-primary" value="Manage">Update payment methods</button>
                    </EditForm>
                    <form method="post">
                    </form>
                    <Table ColumnHeaders="@PaymentMethodsHeaders" EmptyMessage="@PaymentMethodsEmptyMessage">
                        @foreach (var paymentMethod in PaymentMethods!)
                        {
                            <tr>
                                <td class="w-5">
                                    <DynamicComponent Type="paymentMethod.CardIcon" />
                                </td>
                                <td>@paymentMethod.Number</td>
                                @if (paymentMethod.ExpirationDate < DateTime.UtcNow.AddMonths(-1))
                                {
                                    <td>@paymentMethod.ExpirationDate.ToString("MM/yyyy")</td>
                                }
                                else
                                {
                                    <td class="text-danger">@paymentMethod.ExpirationDate.ToString("MM/yyyy")</td>
                                }
                            </tr>
                        }
                    </Table>
                </div>
            </Panel>
        }

        @if (CurrentContext.Organization.HasSubscription)
        {
            <Panel Header="Subscription Cancellation">
                <Alert Style="@ContextualStyles.Info">
                    To cancel your active Passwordless.dev 'Pro' or 'Enterprise' subscription, you will have to delete every application in your organization. Your subscription will be cancelled automatically when the last application has been deleted.
                </Alert>
            </Panel>
        }
    }
</Page>